package com.miao.common.controller;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.miao.common.entity.R;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author MiaoWenqing
 * @description: 发送手机验证码
 * @date 2021/1/18 23:18
 */
@RestController
public class SendCode {

    @Resource(name = "myRedisTemplate")
    private RedisTemplate<String,Object> redisTemplate;

    @GetMapping("common/sendSms")
    public R sendMsg(String phoneNumbers,String msgType) {
        //1 注册 2 重置
        String templateCode=null;
        if ("1".equals(msgType)){
            templateCode="SMS_210071114";
        }else if ("2".equals(msgType)){
            templateCode="SMS_210061346";
        }else {
            return R.error();
        }

        int code = new Random().nextInt(9999);
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G6uQ7WfJG8nGMayH9xd", "FWdqwrupOOMCsPToETs9QGnDuHNW6I");

        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phoneNumbers);
        request.putQueryParameter("SignName", "ABC商城");
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
        CommonResponse response=null;
        try {
             response= client.getCommonResponse(request);
            Map<String,String> map = JSON.parseObject(response.getData(), Map.class);
            if ("OK".equals(map.get("Code"))){
                 this.redisTemplate.opsForValue().set("code:"+phoneNumbers,""+code,15, TimeUnit.MINUTES);
               return new R(2000,"发送成功",null);
            }else if ("isv.MOBILE_NUMBER_ILLEGAL".equals(map.get("Code"))){
                return new R(2001,"非法手机号",null);
            }else if ("isv.BUSINESS_LIMIT_CONTROL".equals(map.get("Code"))){
                return new R(2002,"短信发送超出今日上限",null);
            }else {
                return R.error();
            }

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return new R(2000,"success",response.getData());
    }
}
